package leetcode;

import java.util.Arrays;

public class LongestCommonPrefix {

	public static void main(String[] args) {
		longestCommonPrefix(new String[]{});
	}
	public static String longestCommonPrefix(String[] strs) {
		if (strs == null || strs.length <= 0) {
			return "";
		}
		StringBuilder builder = new StringBuilder();
		Arrays.sort(strs); // sort the string array, then you just can judge the
							// first and last
		char[] first = strs[0].toCharArray();
		char[] last = strs[strs.length - 1].toCharArray();
		for (int i = 0; i < first.length; i++) {
			if (i < last.length && last[i] == first[i]) {
				builder.append(first[i]);
			}else{
                break;
            }
		}
		return builder.toString();
	}
}
